package com.example.learn.service;

import com.example.learn.entity.Account;
import com.example.learn.entity.Statistical;
import com.example.learn.entity.User;
import com.example.learn.mapper.AccountMapper;
import com.example.learn.mapper.StatisticalMapper;
import com.example.learn.utile.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class StatisticalService {
    @Resource
    AccountMapper accountMapper;
    @Resource
    StatisticalMapper statisticalMapper;
    public List<Statistical> getstatistical(Boolean isfamily, User user) {
        Set<String> names=new HashSet();
        if(isfamily){
            //先找出家庭人员
            List<Account>  accounts= accountMapper.getFamilyName(user.getName());

            for (Account account:accounts
            ) {
                names.add(account.getName());
                names.add(account.getAccount());
            }
        }else{
            names.add(user.getName());
        }
        List<Statistical> incomeList=    statisticalMapper.getByName(names);
        return incomeList;
    }

    public String addstatistical(Statistical statistical, User user) {
        if(StringUtils.isEmpty(statistical.getId())){
            statistical.setId(UUID.randomUUID().toString());
            statistical.setName(user.getName());
            statistical.setDisable(true);

            int i=      statisticalMapper.insert(statistical);
        }else {
            statistical.setName(user.getName());
            statistical.setDisable(true);
            statisticalMapper.updateByPrimaryKey(statistical);
        }

        return "ok";
    }

    public String deletestatistical(String id) {
        statisticalMapper.deleteByPrimaryKey(id);
        return  "ok";
    }
}
